<?php

// 
// this simply reads the headers for a given long DMid and returns the short DMid
// transparently passes short ids and returns null (in a JS object) if the page pulls a 404
// if the page isn't found, DMid is an empty string
//
// Sample call:  http://joemaller.com/services/dailymotion_id_translator.php?DMid=2Hsl4PncOh2RYbnSV&_callback=?   (note that jQuery should add the callback)
// 

# set up timer variables
$time = explode(" ", microtime()); 
$stime = $time[1] . substr($time[0],1,strlen($time[0])); 

$headers = get_headers('http://www.dailymotion.com/swf/' . htmlspecialchars($_GET['DMid']), 1);

parse_str(urldecode($headers['Location']), $DMlocation);
preg_match('%video/([^_]+)_%', $DMlocation['click'], $url);

// // go fetch the title
// $options = array( 'http' => array( 'max_redirects' => 10, 'timeout' => 5, 'user_agent' => "Joe's DMid translator (you should offer this) www.joemaller.com" ) );
// $context = stream_context_create( $options );
// 
// $getDmPage = file_get_contents("http://www.dailymotion.com/video/x1m5ox",FALSE,$context,0,1024);
// preg_match('/<title>[vV]ideo (.*) - (?:[^,]+, )+.*<\/title>/', $getDmPage, $DMtitle);
// 
// 
$time2 = explode (" ",microtime ()); 
$etime = $time2[1] . substr($time2[0],1,strlen($time2 [0])); 

// echo htmlspecialchars($_GET['_callback']) . '({"DMid":"' . $url[1] . '", "title":"' . $DMtitle[1] . '", "execution_time":' . round($etime - $stime,4) . ' })';
echo htmlspecialchars($_GET['_callback']) . '({"DMid":"' . $url[1] . '", "execution_time":' . round($etime - $stime,4) . ' })';




?>